package com.fcy.teamwork.business.main.homepage.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.android.api.callback.GetGroupIDListCallback
import cn.jpush.im.android.api.callback.GetGroupInfoCallback
import cn.jpush.im.android.api.callback.GetGroupInfoListCallback
import cn.jpush.im.android.api.model.Conversation
import cn.jpush.im.android.api.model.GroupInfo
import com.fcy.teamwork.App
import com.fcy.teamwork.base.log
import com.fcy.teamwork.bean.Contact
import com.fcy.teamwork.bean.toContact
import com.fcy.teamwork.jmservice.event.UnReadEventListener
import com.fcy.teamwork.pojo.SingleConversation
import com.fcy.teamwork.pojo.TeamWorkResult
import com.fcy.teamwork.repo.RetrofitClient
import kotlinx.coroutines.flow.*
import retrofit2.http.GET
import retrofit2.http.POST
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine


class MessageViewModel : ViewModel() {
    interface MineSingleConversation {
        @GET("/sms/recommend/mineConversations")
        suspend fun loadMine(): TeamWorkResult<List<SingleConversation>>
    }

    private val unReadEventListener by lazy { UnReadEventListener() }
    private val service: MineSingleConversation = RetrofitClient.getService()

    val unReadFlow: SharedFlow<String> by lazy { unReadEventListener.unReadFlow }
    suspend fun loadConversations(): List<Contact> {
        val conversations = getInvolvedGroups()
        val res1 = mutableListOf<Contact>()
        conversations.map {
            Conversation.createGroupConversation(it).toContact()
        }.toCollection(res1)
        val res2 = mutableListOf<Contact>()
        loadSingleConversation().map {
            it.toContact()
        }.toCollection(res2)
        log(res1)
        log(res2)
        return res1 + res2
    }

    private suspend fun getInvolvedGroups() = suspendCoroutine<MutableList<Long>> {
        JMessageClient.getGroupIDList(object : GetGroupIDListCallback() {
            override fun gotResult(p0: Int, p1: String?, p2: MutableList<Long>?) {
                if (p2 == null)
                    it.resume(mutableListOf())
                else {
                    it.resume(p2)
                }
            }

        })
    }

    private suspend fun loadSingleConversation(): List<Conversation> {
        return service.loadMine().data?.map {
            val target = if (it.person1Id == App.person.id) it.person2Id else it.person1Id
            Conversation.createSingleConversation(target.toString())
        }?.toSet()?.toList() ?: emptyList()
    }

//    private suspend fun getGroupInfo(id: Long) = suspendCoroutine<GroupInfo> {
//        JMessageClient.getGroupInfo(id, object : GetGroupInfoCallback() {
//            override fun gotResult(p0: Int, p1: String?, p2: GroupInfo?) {
//                it.resume(p2 ?: throw java.lang.IllegalStateException(""))
//            }
//        })
//    }

    override fun onCleared() {
        super.onCleared()
        unReadEventListener.unregister()
    }
}